package com.facebookproject.graph.getlist;

import static com.facebookproject.Const.*;

import java.io.IOException;
import java.net.MalformedURLException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;

import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;
import com.facebookproject.graph.element.Elements;
import com.facebookproject.loginfb.LoginFacebook;
@SuppressWarnings("deprecation")
public class GetLikes {
	private Facebook facebook = new Facebook(APP_FB_ID);
	public List<Elements> getlist(Context context, boolean b,String fields,String rq){
		facebook = new LoginFacebook().login(context,facebook);
		List<Elements> list = new ArrayList<Elements>();
		try {
			
			Bundle params = new Bundle();
			params.putString("fields",fields);
			params.putString("access_token", facebook.getAccessToken());
			JSONArray ja = Util.parseJson(facebook.request(rq,params,"GET")).getJSONArray("data");
			for (int i = 0; i < ja.length(); i++) {
				if(b)break;
				String id=null,name=null,ele1=null,ele2=null,ele3=null,ele4=null;
				JSONObject tmp = (JSONObject) ja.get(i);
				id = tmp.getString("id");
				name = tmp.getString("name");
				if(tmp.has("category"))ele1 = tmp.getString("category");
				if(tmp.has("likes"))ele2 = pluxcommas(tmp.getString("likes")) + " people also like this.";
				if(tmp.has("talking_about_count"))ele3 = pluxcommas(tmp.getString("talking_about_count")) + " talking about.";
				Elements ele = new Elements(id, name, ele1, ele2, ele3, ele4);
				list.add(ele);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (FacebookError e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}
	private String pluxcommas(String str) {
		double d = Double.parseDouble(str);
		NumberFormat formatter = new DecimalFormat("###,###,###");
		str = formatter.format(d);
		return str;
	}
}
